import { Controller, Post, Get, ParseIntPipe, Body, BadRequestException, Param } from '@nestjs/common';
import { ProductsService } from './products.service';
import { success } from '@/utils';

@Controller('products')
export class ProductsController {
    constructor(private readonly productsService: ProductsService) { }
    @Post('list')
    async list(@Body() info: any) {
        try {
            const res = await this.productsService.findAll(info)
            return success(res, '获取成功');
        } catch (error) {
            throw new BadRequestException('参数不正确');
        }
    }
    @Post('listBy')
    async listBy(@Body() info: any) {
        try {
            const res = await this.productsService.findBy(info)
            return success(res, '获取成功');
        } catch (error) {
            throw new BadRequestException('参数不正确');
        }
    }
    @Post('edit')
    async edit(@Body() info): Promise<any> {
        try {
            let res: any; // 明确声明变量类型为 any
            if (info.id) {
                res = await this.productsService.update(info.id, info); // update 方法返回值
            } else {
                res = await this.productsService.add(info); // add 方法返回值
            }
            return success(res, info.id ? '修改成功' : '添加成功'); // 统一返回结果
        } catch (error) {
            throw new BadRequestException(error); // 捕获异常并抛出错误
        }
    }
    @Get('detail/:id')
    async detail(@Param('id',ParseIntPipe) id: number){
        const res =  await this.productsService.detail(id)
        return success(res, '获取成功');
    }
}